<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Course;
use App\Models\ArticleCategory;
use App\Models\CourseCategory;
use App\Models\FirmProfession;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class CourseController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Course(['user:id,avatar,nickname','category:id,name']), function (Grid $grid) {
            $grid->column('id')->sortable();

            $grid->column('sort');
            $grid->column('user.avatar','用户头像')->image('',80,80);
            $grid->column('user.nickname','用户昵称');
            $grid->column('cover','课程封面')->image(100,80);
            $grid->column('category.name','所属行业');
            $grid->column('name');
            $grid->column('status')
                ->using([0 => '隐藏',1 => '显示'])
                ->label([0 => 'danger',1 => 'success']);
            $grid->column('audit_status')
                ->using([1 => '待审核',2 => '已通过',3 => '已拒绝'])
                ->label([1 => 'danger',2 => 'success',3 => 'warning']);
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            $grid->fixColumns(1);

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->like('name')->width(3);
                $filter->equal('firm_profession_id')->select(FirmProfession::getOption())->width(3);

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Course(), function (Show $show) {
            $show->field('id');
            $show->field('sort');
            $show->field('user_id');
            $show->field('name');
            $show->field('content');
            $show->field('status');
            $show->field('audit_status');
            $show->field('reject_reason');
            $show->field('sale_type');
            $show->field('price');
            $show->field('points');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Course(), function (Form $form) {
            $form->display('id');
            $form->number('sort')
                ->min(0)
                ->help('数字越小排序越靠前')
                ->required();
            $form->select('firm_profession_id')
                 ->options(FirmProfession::getOption())
                 ->required();
            $form->text('name')
                 ->maxLength(255)
                 ->required();

            $form->image('cover','课程封面')
                ->disk('oss')
                ->autoUpload()
                ->removable(false)
                ->uniqueName()
                ->saveFullUrl()
                ->required();

            $form->editor('content')
                 ->required();
            $form->radio('sale_type')
                ->options([1 => '现金',2 => '积分',3 => '免费'])
                ->default(1)
                ->when(1,function (Form $form){
                    $form->currency('price')
                        ->rules('required_if:sale_type,1',['required_if' => '价格不能为空'])
                        ->symbol('￥')
                        ->saving(function ($value){
                           return (float)$value;
                        });
                })->when(2,function (Form $form){
                    $form->number('points')
                        ->min(0)
                        ->rules('required_if:sale_type,2',['required_if' => '积分不能为空'])
                        ->saving(function ($value){
                            return (int)$value;
                        });
                })->required();

            $form->radio('status')
                ->options([1 => '显示',0 => '隐藏'])
                ->default(1)
                ->required();
            $form->radio('audit_status')
                ->options([1 => '待审核',2 => '已通过',3 => '已拒绝'])
                ->default(1)
                ->when(3,function (Form $form){
                    $form->textarea('reject_reason')
                        ->rules('max:255',['max' => '最多500字'])
                        ->saving(function ($value){
                            return (string)$value;
                        });
                })->required();


            $form->saved(function (Form $form){
                if ($form->input('user_id') && $form->input('audit_status')==2){
                    event(new \App\Events\Message([
                        'user_id' =>  $form->input('user_id'),
                        'type' => 2,
                        'content' => '恭喜您发布的课程已通过审核'
                    ]));
                }

                if ($form->input('user_id') && $form->input('audit_status')==3){
                    event(new \App\Events\Message([
                        'user_id' =>  $form->input('user_id'),
                        'type' => 2,
                        'content' => '您发布的课程未通过审核'
                    ]));
                }

            });


            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
